/* Lupton Teigen 2025 "Base Assumptions"

Because the cumulative file does not include everything needed, 
this script looks for
downloaded Harvard Dataverse CES files in Stata format (xxx.dta) 
and then a destination file to put the 
working cross sections in as they're trimmed down. File names in 
the script below are taken directly from Dataverse; 
they're not renamed arbitrarily. These might need adjusting 
if the CES or Dataverse updates or 
posts corrections or errata etc.  Note: CES (and CCES) data do not 
have consistent variable names or codes 
across the cross sections. 

Filepath: this script assumes a single "folderfile" (local macro) 
to a single place where the twelve different Stata-formatted files are 
stored: nine cross sections of CES data from the Harvard Dataverse
 (2006 2008 2010 2012 2014 2016 2018 2020 2022) which can be 
found at https://cces.gov.harvard.edu/ , a .dta file with zipcodes 
and their respective latitudes and longitudes (zipcode_latlong.dta), 
a .dta file with racial and ethnic composition of 
zipcodes (zipdemographicdata.dta), and a .dta file 
with the population density of zipcodes (simplemapdotcomdata_pared.dta).  
There needs to be an empty folder named "cces_work" too.
Assuming installation of "geodist" Stata ADO 

*/

clear
set more off

*pathname needs customization
local folderfile e:\Dropbox\replicate_bases\
local workingcces e:\Dropbox\replicate_bases\cces_work\

/****************2006*****************/
use "`folderfile'cces_2006_common.dta"
keep if v4001==1
gen year=2006
rename v1001 weight
rename v3007 pid7 
rename v2021 ideo5
rename v2032 faminc
rename v2005 race
rename v2018 educ
rename v3081 military_serving
rename v3083 military_served
rename v2004 gender
rename v2022 relig
rename v2070 bornagain 
rename v2020 birthyear
rename v2019 marstat 
rename v1002 inputstate_string
rename v5047 lookupzip_post
keep inputstate lookupzip_post marstat birthyear bornagain relig gender military_served military_serving educ race faminc ideo5 pid7 weight year vote_gen06 
compress
save "`workingcces'cces2006_w.dta" , replace
clear

/***********************2008*****************/
use "`folderfile'cces_2008_common.dta"
drop if V400==2
gen year=2008
rename V201 weight
rename CC307a pid7
rename V243 ideo5
rename V246 faminc
rename V211 race
rename V213 educ
rename CC328_1 military_serving
rename CC328_3 military_served
rename V208 gender
rename V215 bornagain 
rename V219 relig
rename V207 birthyear
rename V214 marstat
rename CC410 presvote /* 1 JMcC 2 BO */
tostring V253, gen(lookupzip_post) format(%05.0f)
destring V251, gen(inputstate_byte)
keep presvote inputstate_byte lookupzip_post marstat birthyear bornagain relig gender military_served military_serving educ race faminc ideo5 pid7 weight year 
compress
save "`workingcces'cces2008_w.dta" , replace
clear

/************2010************* */
use "`folderfile'cces_2010_common_validated.dta"
drop if V403==2
gen year=2010
rename V101 weight
rename V212d pid7
rename V243 ideo5
rename V246 faminc
rename V211 race
rename V213 educ
rename V252 military_serving
rename V254 military_served
rename V219 relig
rename V215 bornagain
rename V207 birthyear
rename V208 gender
rename V214 marstat
rename V206 inputstate_byte
rename V201_post lookupzip_post
keep inputstate_byte lookupzip_post marstat birthyear bornagain relig gender military_served military_serving educ race faminc ideo5 pid7 weight year 
compress
save "`workingcces'cces2010_w.dta" , replace
clear

/************2012************* */
use "`folderfile'commoncontent2012.dta"
drop if starttime_post==.
gen year=2012
rename V103 weight
rename milstat_1 military_serving
rename milstat_3 military_served
rename pew_bornagain bornagain
rename religpew relig
rename birthyr birthyear
rename CC410a presvote /* 1 BO 2 MR  */
rename inputstate_post inputstate_byte
keep presvote inputstate_byte lookupzip_post marstat birthyear bornagain relig gender military_served military_serving educ race faminc ideo5 pid7 weight year 
compress
save "`workingcces'cces2012_w.dta" , replace
clear

/************2014************* */

use "`folderfile'CCES14_Common_Content_Validated.dta"
drop if tookpost==0
gen year=2014
rename milstat_1 military_serving
rename milstat_3 military_served
rename religpew relig
rename pew_bornagain bornagain
rename birthyr birthyear
drop inputstate
rename inputstate_post inputstate_byte
keep inputstate_byte lookupzip_post marstat birthyear bornagain relig gender military_served military_serving educ race faminc ideo5 pid7 weight year 
compress
save "`workingcces'cces2014_w.dta" , replace
clear

/************2016************* */

use "`folderfile'CCES16_Common_OUTPUT_Feb2018_VV.dta"
drop if tookpost==0
gen year=2016
rename commonweight weight 
rename milstat_1 military_serving
rename milstat_3 military_served
rename pew_bornagain bornagain
rename religpew relig
rename birthyr birthyear
rename CC16_410a presvote /* 1 DJT 2 HRC */
rename inputstate_post inputstate_byte
keep presvote inputstate_byte lookupzip_post marstat birthyear bornagain relig gender military_served military_serving educ race faminc ideo5 pid7 weight year 
compress
save "`workingcces'cces2016_w.dta" , replace
clear

/************2018************* */

use "`folderfile'cces18_common_vv.dta"
keep if tookpost==2
rename commonweight weight 
gen year=2018
rename milstat_1 military_serving
rename milstat_3 military_served
rename birthyr birthyear
rename pew_bornagain bornagain
rename religpew relig
rename inputstate_post inputstate_byte

keep inputstate_byte lookupzip_post marstat birthyear bornagain relig gender military_served military_serving educ race faminc ideo5 pid7 weight year 
compress
save "`workingcces'cces2018_w.dta" , replace
clear

/*************2020************* */

use "`folderfile'CCES22_Common_OUTPUT_vv_topost.dta"
keep if tookpost==2
rename commonpostweight weight
gen year=2020
rename milstat_1 military_serving
rename milstat_3 military_served
rename birthyr birthyear
rename pew_bornagain bornagain
rename religpew relig
rename inputstate_post inputstate_byte
rename gender4_post gender 
recode gender 3=. 4=.
rename presvote20post presvote /* 1 JB 2 DJT */
keep presvote inputstate_byte lookupzip_post marstat birthyear bornagain relig gender military_served military_serving educ race faminc ideo5 pid7 weight year 
compress
save "`workingcces'cces2020_w.dta" , replace 
clear

/*******2022 CCES**** */

use "`folderfile'CCES22_Common_OUTPUT_vv_topost.dta"
keep if tookpost==2
rename commonweight weight
gen year=2022
rename milstat_1 military_serving
rename milstat_3 military_served
rename birthyr birthyear
rename pew_bornagain bornagain
rename religpew relig
rename inputstate_post inputstate_byte
/* CES 2022 expanded gender identity options*/
/* prior years male 1 ; female 2 need to truncate for consistency */
gen gender = . 
recode gender .=1 if gender4==1
recode gender .=2 if gender4==2
keep inputstate_byte lookupzip_post marstat birthyear bornagain relig gender military_served military_serving educ race faminc ideo5 pid7 weight year 
compress
save "`workingcces'cces2022_w.dta" , replace 
clear

/* "Assemble" Pooled Cross Sections 
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣤⣶⣶⣶⣿⣿⣿⣿⣿⣿⣿⣿⣶⣶⣶⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣦⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠿⠿⠿⠿⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⡿⠿⠛⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠙⠛⠿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣰⣾⣿⣿⣿⣿⣿⣿⠿⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⢿⣿⣿⣿⣿⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⡿⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣠⣤⣤⣤⣤⣤⣤⣄⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣿⣿⣿⣿⣿⣿⣷⡄⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣿⡿⠋⠀⠀⠀⠀⠀⠀⠀⣀⣤⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣤⣀⠀⠀⠀⠀⠀⠀⠀⠙⣿⣿⣿⣿⣿⣿⣦⠀⠀⠀⠀⠀
⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿⠟⠀⠀⠀⠀⠀⠀⢀⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣦⡀⠀⠀⠀⠀⠀⠈⠻⣿⣿⣿⣿⣿⣷⡀⠀⠀⠀
⠀⠀⢀⣾⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⠿⠟⠛⠋⠉⠉⠉⠉⠙⠛⠻⢿⣿⣿⣿⣿⣿⣿⣿⣦⡀⠀⠀⠀⠀⠀⠙⣿⣿⣿⣿⣿⣷⡀⠀⠀
⠀⠀⣾⣿⣿⣿⣿⣿⠁⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿⣿⣿⠟⠋⠁⠀⠀⠀⠀⠀⢰⡄⠀⠀⠀⠀⠀⠈⠙⢿⣿⣿⣿⣿⣿⣿⣄⠀⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⣧⠀⠀
⠀⣸⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⡟⠁⠀⠀⠀⠀⠀⠀⠀⢀⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣿⣿⣿⣿⣿⣦⠀⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⣇⠀
⢀⣿⣿⣿⣿⣿⡏⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⣿⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⠀
⢸⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⢰⣿⣿⣿⣿⣿⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠈⣿⣿⣿⣿⣿⡇
⣿⣿⣿⣿⣿⡏⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⡿⠀⠈⠛⢿⣿⣿⣶⣶⣶⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠋⠀⠀⣿⣿⣿⣿⣿⣇⠀⠀⠀⠀⠀⢿⣿⣿⣿⣿⣇
⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠈⠻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠛⠁⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠉⢻⣿⣿⣿⣿⣿⣿⣿⣿⡟⠉⠀⠀⠀⠀⠀⠀ ⠀⣼⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿
⢿⣿⣿⣿⣿⣇⠀⠀⠀⠀⠀⢻⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀ ⠀⠀⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⡇
⢸⣿⣿⣿⣿⣿⡀⠀⠀⠀⠀⠸⣿⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⠀⣸⣿⣿⣿⡿⠋⠙⢿⣿⣿⣿⡇⠀⠀⠀⠀ ⠀⠀⣸⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⡇
⠈⣿⣿⣿⣿⣿⣇⠀⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⣆⠀⠀⠀⠀⢠⣿⡿⠛⠁⠀⠀⠀⠀⠈⠛⢿⣿⡄⠀⠀⠀ ⠀⣼⣿⣿⣿⣿⣿⠏⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⠀
⠀⢹⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠻⣿⣿⣿⣿⣿⣷⣄⠀⠀⠞⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠳⠀  ⠀⣠⣾⣿⣿⣿⣿⣿⠏⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿⣿⠇⠀
⠀⠀⢿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠙⣿⣿⣿⣿⣿⣿⣷⣤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀  ⠀⠀⠀⣀⣤⣾⣿⣿⣿⣿⣿⡿⠋⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⡟⠀⠀
⠀⠀⠈⢿⣿⣿⣿⣿⣿⣄⠀⠀⠀⠀⠀⠈⠻⣿⣿⣿⣿⣿⣿⣿⣷⣶⣤⣤⣤⣄⣠⣤⣤⣤⣶⣿⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿⣿⡟⠀⠀⠀
⠀⠀⠀⠈⢿⣿⣿⣿⣿⣿⣦⡀⠀⠀⠀⠀⠀⠈⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⠁⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣿⡟⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠻⣿⣿⣿⣿⣿⣿⣦⡀⠀⠀⠀⠀⠀⠀⠈⠛⠻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠟⠋⠁⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⠏⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠘⢿⣿⣿⣿⣿⣿⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠛⠛⠛⠋⠉⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣴⣿⣿⣿⣿⣿⣿⡿⠁⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣷⣦⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣴⣾⣿⣿⣿⣿⣿⣿⡿⠋⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣦⣤⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣤⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⠟⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣶⣶⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠻⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠛⠛⠿⠿⠿⠿⣿⣿⣿⣿⠿⠿⠿⠿⠛⠛⠉⠁⠀
*/
clear

local workingcces e:\Dropbox\replicate_bases\cces_work\

use "`workingcces'cces2006_w.dta" 
local yearcount = 2008
while `yearcount'<=2023 {
	append using "`workingcces'cces`yearcount'_w.dta"
	local yearcount = `yearcount'+2
}

/*Recodes */


 /******Two-Party Presidential Vote************* */
/* This is inconsistent year-to-year */ 

#delimit ;

gen gop2ptyvote = . ;
	recode gop2ptyvote .=1 if year==2008 & presvote==1 ;
	recode gop2ptyvote .=0 if year==2008 & presvote==2 ;
	recode gop2ptyvote .=1 if year==2012 & presvote==2 ; 
	recode gop2ptyvote .=0 if year==2012 & presvote==1 ;
	recode gop2ptyvote .=1 if year==2016 & presvote==1 ;
	recode gop2ptyvote .=0 if year==2016 & presvote==2 ;
	recode gop2ptyvote .=0 if year==2020 & presvote==1 ;
	recode gop2ptyvote .=1 if year==2020 & presvote==2 ;
	replace gop2ptyvote=. if (year==2006 | year==2010 | year==2014 | year==2018) ;
	label variable gop2ptyvote "Pres. Vote Choice" ;
	label define gop2ptyvote 0 "Dem" 1 "GOP" ;
	label values gop2ptyvote gop2ptyvote ;
	
/*******Party Identification (both w/ and w/o Leaners)****** */
gen goppid = .;
	recode goppid .=1 if pid7==6 | pid7==7;
	recode goppid .=0 if pid7<6 ;
	label variable goppid "Republican Identifier wo/Leaners";
	label define goppid 0 "Not GOP" 1 "GOP";
	label values goppid goppid ;

gen goppid_l = .;
	recode goppid_l .=1 if pid7==5 | pid7==6 | pid7==7;
	recode goppid_l .=0 if pid7<5 ;
	label variable goppid_l "Republican Identifier w/Leaners";
	label define goppid_l 0 "Not GOP" 1 "GOP";
	label values goppid_l goppid_l ;

gen dempid = . ;
	recode dempid .=1 if pid7==1 | pid7==2 ;
	recode dempid .=0 if pid7==3 | pid7==4 | pid7==5 | pid7==6 | pid7==7;
	label variable dempid "Democratic Identifier wo/Leaners";
	label define dempid 0 "Not Dem" 1 "Dem";
	label values dempid dempid ;
	
gen dempid_l = . ;
	recode dempid_l .=1 if pid7==1 | pid7==2 | pid7==3;
	recode dempid_l .=0 if pid7==4 | pid7==5 | pid7==6 | pid7==7;
	label variable dempid_l "Democratic Identifier w/Leaners";
	label define dempid_l 0 "Not Dem" 1 "Dem";
	label values dempid_l dempid_l ;
		
/*******Race ethnicity****** */
gen whitenonhisp = 0;
	recode whitenonhisp 0=1 if race==1;
	label variable whitenonhisp "White non-Hispanic" ;
	label define whitenonhisp 0 "Race/Eth Minority" 1 "Non-Hispanic White" ;
	label values whitenonhisp whitenonhisp ;

/********Age****** */
gen age=year-birthyear;
	gen bage = .;
	recode bage .=2 if age>19 & age<30;
	recode bage .=3 if age>29 & age<40;
	recode bage .=4 if age>39 & age<50;
	recode bage .=5 if age>49 & age<60;
	recode bage .=6 if age>59 & age<70;
	recode bage .=7 if age>69 & age<80;
	label variable bage "Bracketed Age" ;
	label define bage 2 "20s" 3 "30s" 4 "40s" 5 "50s" 6 "60s" 7 "70+";
	label values bage bage ;
	gen int birthdecade = . ;
	recode birthdecade .=2 if birthyear>=1920 & birthyear<1930;
	recode birthdecade .=3 if birthyear>=1930 & birthyear<1940;
	recode birthdecade .=4 if birthyear>=1940 & birthyear<1950;
	recode birthdecade .=5 if birthyear>=1950 & birthyear<1960;
	recode birthdecade .=6 if birthyear>=1960 & birthyear<1970;
	recode birthdecade .=7 if birthyear>=1970 & birthyear<1980;
	recode birthdecade .=8 if birthyear>=1980 & birthyear<1990;
	recode birthdecade .=9 if birthyear>=1990 & birthyear<2000;
	label variable birthdecade "Birth Decade" ;
	label define birthdecade 2 "1920s" 3 "1930s" 4 "1940s" 5 "1950s" 6 "1960s" 7 "1970s" 8 "1980s" 9 "1990s" ;
	label values birthdecade birthdecade ;

/*******State ****** */
replace inputstate_byte=1 if inputstate_string=="AL" & year<2009 ;
	replace inputstate_byte=2 if inputstate_string=="AK" & year<2009 ;
	replace inputstate_byte=4 if inputstate_string=="AZ" & year<2009 ;
	replace inputstate_byte=5 if inputstate_string=="AR" & year<2009 ;
	replace inputstate_byte=6 if inputstate_string=="CA" & year<2009 ;
	replace inputstate_byte=8 if inputstate_string=="CO" & year<2009 ;
	replace inputstate_byte=9 if inputstate_string=="CT" & year<2009 ;
	replace inputstate_byte=10 if inputstate_string=="DE" & year<2009 ;
	replace inputstate_byte=11 if inputstate_string=="DC" & year<2009 ;
	replace inputstate_byte=12 if inputstate_string=="FL" & year<2009 ;
	replace inputstate_byte=13 if inputstate_string=="GA" & year<2009 ;
	replace inputstate_byte=15 if inputstate_string=="HI" & year<2009 ;
	replace inputstate_byte=16 if inputstate_string=="ID" & year<2009 ;
	replace inputstate_byte=17 if inputstate_string=="IL" & year<2009 ;
	replace inputstate_byte=18 if inputstate_string=="IN" & year<2009 ;
	replace inputstate_byte=19 if inputstate_string=="IA" & year<2009 ;
	replace inputstate_byte=20 if inputstate_string=="KS" & year<2009 ;
	replace inputstate_byte=21 if inputstate_string=="KY" & year<2009 ;
	replace inputstate_byte=22 if inputstate_string=="LA" & year<2009 ;
	replace inputstate_byte=23 if inputstate_string=="ME" & year<2009 ;
	replace inputstate_byte=24 if inputstate_string=="MD" & year<2009 ;
	replace inputstate_byte=25 if inputstate_string=="MA" & year<2009 ;
	replace inputstate_byte=26 if inputstate_string=="MI" & year<2009 ;
	replace inputstate_byte=27 if inputstate_string=="MN" & year<2009 ;
	replace inputstate_byte=28 if inputstate_string=="MS" & year<2009 ;
	replace inputstate_byte=29 if inputstate_string=="MO" & year<2009 ;
	replace inputstate_byte=30 if inputstate_string=="MT" & year<2009 ;
	replace inputstate_byte=31 if inputstate_string=="NE" & year<2009 ;
	replace inputstate_byte=32 if inputstate_string=="NV" & year<2009 ;
	replace inputstate_byte=33 if inputstate_string=="NH" & year<2009 ;
	replace inputstate_byte=34 if inputstate_string=="NJ" & year<2009 ;
	replace inputstate_byte=35 if inputstate_string=="NM" & year<2009 ;
	replace inputstate_byte=36 if inputstate_string=="NY" & year<2009 ;
	replace inputstate_byte=37 if inputstate_string=="NC" & year<2009 ;
	replace inputstate_byte=38 if inputstate_string=="ND" & year<2009 ;
	replace inputstate_byte=39 if inputstate_string=="OH" & year<2009 ;
	replace inputstate_byte=40 if inputstate_string=="OK" & year<2009 ;
	replace inputstate_byte=41 if inputstate_string=="OR" & year<2009 ;
	replace inputstate_byte=42 if inputstate_string=="PA" & year<2009 ;
	replace inputstate_byte=44 if inputstate_string=="RI" & year<2009 ;
	replace inputstate_byte=45 if inputstate_string=="SC" & year<2009 ;
	replace inputstate_byte=46 if inputstate_string=="SD" & year<2009 ;
	replace inputstate_byte=47 if inputstate_string=="TN" & year<2009 ;
	replace inputstate_byte=48 if inputstate_string=="TX" & year<2009 ;
	replace inputstate_byte=49 if inputstate_string=="UT" & year<2009 ;
	replace inputstate_byte=50 if inputstate_string=="VT" & year<2009 ;
	replace inputstate_byte=51 if inputstate_string=="VA" & year<2009 ;
	replace inputstate_byte=53 if inputstate_string=="WA" & year<2009 ;
	replace inputstate_byte=54 if inputstate_string=="WV" & year<2009 ;
	replace inputstate_byte=55 if inputstate_string=="WI" & year<2009 ;
	replace inputstate_byte=56 if inputstate_string=="WY" & year<2009 ;

label copy V206 state_code ;
label values inputstate_byte state_code ;

/*******Male dummy****** */
generate male = .;
	recode male .=0 if gender==2;
	recode male .=1 if gender==1;
	label variable male "Gender" ;
	label define male 0 "Female" 1 "Male" ;
	label values male male ;
	
/*******Income****** */
/* $70k family income control */
/* scale and var name are inconsistent for family income var */
/* 2022: same as '18 */
/* 2020: same as '18 */
/* 2018: ("faminc_new" renamed above), 16 top "$500+", 8 is $70-79k */
/* 2016: 14 top value "$150+", 10 is "$70-79.9k" */
/* 2014: 13 appears to be top useful value "$200-249k", 8 is "$70" */
/* 2012: 16 top code ($500k+), 8 is $70k */
/* 2010: ("V246" renamed above) 14 top code ($150k+), 10 is $70k */
/* 2008: ("V246" renamed above) 14 top code ($150k+), 10 is $70k */
/* 2006: ("v2032" renamed above) 14 top code ($150k+), 10 is $70k */
gen svyk = . ;
	recode svyk .=0 if (year==2006) & (faminc<=9) ;
	recode svyk .=1 if (year==2006) & (faminc>=10 & faminc<=14) ;
	recode svyk .=0 if (year==2008 | year==2010 ) & (faminc<11) ;
	recode svyk .=1 if (year==2008 | year==2010 ) & (faminc>9 & faminc<15) ;
	recode svyk .=0 if year==2012 & (faminc<8);
	recode svyk .=1 if year==2012 & (faminc>7 & faminc<97);
	recode svyk .=0 if year==2014 & faminc<8;
	recode svyk .=1 if year==2014 & faminc>7 & faminc<97;
	recode svyk .=0 if year==2016 & faminc<8;
	recode svyk .=1 if year==2016 & faminc>7 & faminc<97 ;
	recode svyk .=0 if year==2018 & faminc_new<8 ;
	recode svyk .=1 if year==2018 & faminc_new>7 & faminc_new<17 ;
	recode svyk .=0 if year==2020 & faminc_new<8 ;
	recode svyk .=1 if year==2020 & faminc_new>7 & faminc_new<96 ;
	recode svyk .=0 if year==2022 & faminc_new<8 ;
	recode svyk .=1 if year==2022 & faminc_new>7 & faminc_new<96 ;
	label variable svyk "Family Income" ;
	label define svyk 0 "<$70k" 1 ">$70k" ;
	label values svyk svyk ;
	
/*******Education****** */
gen ba = . ;
	recode ba .=0 if educ<5 ;
	recode ba .=1 if educ==5 | educ==6 ;
	label variable ba "Education" ;
	label define ba 0 "Less than BA" 1 "BA or higher" ;
	label values ba ba;

/*******Military Service Experience****** */
gen vet = . ;
	recode vet .=0 if military_served==2 ;
	recode vet .=1 if military_served==1 ;
	label variable vet "Military Service Experience" ;
	label define vet 0 "Non-Vet" 1 "Vet" ;
	label values vet vet ;

gen curr_serv = . ;
	recode curr_serv .=0 if military_serving==2 ;
	recode curr_serv .=1 if military_serving==1 ;
	label variable curr_serv "Currently Serving Military" ;
	label define curr_serv 0 "Civilian" 1 "Serving" ;
	label values curr_serv curr_serv ;
	
gen nevermil = . ;
	recode nevermil .=1 if military_serving==2 & military_served==2 ;
	recode nevermil .=0 if military_serving==1 | military_served==1 ;
	label define nevermil 0 "Served or Serving" 1 "Never Served" ;
	label values nevermil nevermil ;
	
/*******Evangelical****** */
gen evangc = . ;
	recode evangc .=0 if bornagain==2 ;
	recode evangc .=1 if bornagain==1 ;
	label variable evangc "Evangelical Christian" ;
	label define evangc 0 "Non EC" 1 "EC" ;
	label values evangc evangc ;

/*******Marital Status****** */

gen marrspouse = . ;
	recode marrspouse .=0 if (marstat>2 & marstat<7);
	recode marrspouse .=1 if marstat==1 ;
	label variable marrspouse "Marital Status" ;
	label define marrspouse 0 "Not Married" 1 "Married w/Spouse" ;
	label values marrspouse marrspouse ;
	
/*********Zip Code level info*********** */

gen byte notnumeric = real(lookupzip)==. ;
	tab year notnumeric ;
	drop if notnumeric==1 ;
	drop notnumeric ;
	destring lookupzip, gen(zipcode) ;
	format %05.0f zipcode ;

/***Geolocate each reponsdent by imposing their zipcode's centroid to them */	
merge m:m zipcode using "`folderfile'zipcode_latlong.dta" , gen(zipdensitymergecode1);
	drop if zipdensitymergecode1!=3 ;
	gen outerlimit=100;
	scatter lat lng, msize(vtiny) scheme(s1mono) text( 40 -100 "USA") yscale(off) xscale(off);

/***Add zipcode level demographics *** */	
sort zipcode;
	merge m:m zipcode  using "`folderfile'zipdemographicdata.dta" , gen(zipdensitymergecode2);
	drop if zipdensitymergecode2!=3 ;	

/***Add zipcode level population density*** */	
sort zipcode;
	merge m:m zipcode using "`folderfile'simplemapdotcomdata_pared.dta" , gen(simplemapmergecode3);
	drop if simplemapmergecode3!=3 ;
	drop if dens_metric_simplemapdotcom==0 ; /* Outlying addresses or other weird zipcodes like 30 Rock NYC */
	gen dens_quartiles=.;
	recode dens_quartiles .=1 if dens_metric_simplemapdotcom<=103.7;
	recode dens_quartiles .=2 if dens_metric_simplemapdotcom>103.7 & dens_metric_simplemapdotcom<=515.7 ;
	recode dens_quartiles .=3 if dens_metric_simplemapdotcom>515.7 & dens_metric_simplemapdotcom<=1458.3 ;
	recode dens_quartiles .=4 if dens_metric_simplemapdotcom>1458.3 & dens_metric_simplemapdotcom<10000000 ;
	label define dens_quartiles 1 "Q1" 2 "Q2" 3 "Q3" 4 "Q4" ;
	label values dens_quartiles dens_quartiles ;
	label variable dens_quartiles "Population Density";
	
/**Distance between each respondent and each military base
These are numbered arbitarily, roughly in alphabetical order by state. Not all them have sufficient sample sizes for the 
sixty mile circle analysis. */

/*1  Maxwell AFB AL */
geodist lat lng 32.3839 -86.3568, gen(maxwellafbdistprecise) ;
gen Wmaxwellafbdist = round(maxwellafbdistprecise, 1);
gen Qmaxwellafbnear = outerlimit-maxwellafbdist ;
recode Qmaxwellafbnear -100000/0=0 ;

/*2 Luke AFB  AZ */
geodist lat lng 33.5401  -112.3584 , gen(lukeafbdistprecise) ;
gen Wlukeafbdist = round(lukeafbdistprecise, 1) ;
gen Qlukeafbnear = outerlimit-lukeafbdist ;
recode Qlukeafbnear -100000/0=0;

/*3 Fort Novosel née Fort Rucker AL FL GA */
geodist lat lng 31.3380 -85.7085, gen(fortnovoseldistprecise) ;
gen Wfortnovoseldist = round(fortnovoseldistprecise, 1);
gen Qfortnovoselnear = outerlimit-fortnovoseldist ;
recode Qfortnovoselnear -100000/0=0; 
	
/*4 Davis Monthan AFB AZ */
geodist lat lng 32.1810 -110.8807 , gen(davismonthanafbdistprecise) ;
gen Wdavismonthanafbdist = round(davismonthanafbdistprecise, 1);
gen Qdavismonthanafbnear = outerlimit-davismonthanafbdist ;
recode Qdavismonthanafbnear -100000/0=0 ;
		
/*5 Fort Huachuca AZ NM */
geodist lat lng 31.5579 -110.3544, gen(forthuachucadistprecise);
gen Wforthuachucadist = round(forthuachucadistprecise, 1);
gen Qforthuachucanear = outerlimit-forthuachucadist ;
recode Qforthuachucanear -100000/0=0 ;	

/*6 Fort Richardson AK */
geodist lat lng 61.2549 -149.6953 , gen(fortrichardsondistprecise);
gen Wfortrichardsondist=round(fortrichardsondistprecise,1);
gen Qfortrichardsonnear = outerlimit-fortrichardsondist;
recode Qfortrichardsonnear -100000/0=0 ;

/*7 Fort Wainwright */
geodist lat lng 64.8414 -147.6079, gen(fortwainwrightdistprecise);
gen Wfortwainwrightdist=round(fortwainwrightdistprecise, 1);
gen Qfortwainwrightnear= outerlimit-fortwainwrightdist;
recode Qfortwainwrightnear -100000/0=0;

/*8 Elmendorf AFB */
geodist lat lng 61.2436 -149.8230 , gen(elmendorfafbdistprecise);
gen Welmendorfafbdist=round(elmendorfafbdistprecise,1);
gen Qelmendorfafbnear=outerlimit-elmendorfafbdist;
recode Qelmendorfafbnear -100000/0=0;

/*9 Little Rock AFB (AR) */
geodist lat lng 34.8963 -92.1423 , gen(littlerockafbdistprecise);
gen Wlittlerockafbdist=round(littlerockafbdistprecise,1);
gen Qlittlerockafbnear=outerlimit-littlerockafbdist;
recode Qlittlerockafbnear -100000/0=0;

/* 10 Beale AFB (CA & NV) */
geodist lat lng 39.1253 -121.4210, gen(bealeafbdistprecise); /*corrected!*/
gen Wbealeafbdist=round(bealeafbdistprecise,1);
gen Qbealeafbnear=outerlimit-bealeafbdist;
recode Qbealeafbnear -100000/0=0;

/*11 Camp Parks RFTA (CA) */
geodist lat lng 37.7161 -121.9036, gen(campparksdistprecise);
gen Wcampparksdist=round(campparksdistprecise,1);
gen Qcampparksnear=outerlimit-campparksdist;
recode Qcampparksnear -100000/0=0;

/*12 Camp Pendleton (CA) */
geodist lat lng 33.2147 -117.3875, gen(camppendletondistprecise);
gen Wcamppendletondist=round(camppendletondistprecise,1);
gen Qcamppendletonnear=outerlimit-camppendletondist;
recode Qcamppendletonnear -100000/0=0;

/*13 Edwards AFB (CA) */
geodist lat lng 34.9250 -117.8957, gen(edwardsafbdistprecise);
gen Wedwardsafbdist=round(edwardsafbdistprecise,1);
gen Qedwardsafbnear=outerlimit-edwardsafbdist;
recode Qedwardsafbnear -100000/0=0;

/*14 Fort Hunter Liggett (CA) */
geodist lat lng 36.0083 -121.2384, gen(forthunterliggettdistprecise);
gen Wforthunterliggettdist=round(forthunterliggettdistprecise,1);
gen Qforthunterliggettnear=outerlimit-forthunterliggettdist;
recode Qforthunterliggettnear -100000/0=0;

/*15 Fort Irwin (CA & NV ) */
*gen fortirwinlat = 35.2632 ;
*gen fortirwinlng = -116.6886 ;
geodist lat lng 35.2632 -116.6886, gen(fortirwindistprecise);
gen Wfortirwindist=round(fortirwindistprecise,1);
gen Qfortirwinnear=outerlimit-fortirwindist;
recode Qfortirwinnear -100000/0=0;

/*16 Los Angeles AFB (CA) */
geodist lat lng 33.9183 -118.3817, gen(losangelesafbdistprecise);
gen Wlosangelesafbdist=round(losangelesafbdistprecise,1);
gen Qlosangelesafbnear=outerlimit-losangelesafbdist;
recode Qlosangelesafbnear -100000/0=0;

/*17 Naval Base Coronado (CA) */
geodist lat lng 32.6737 -117.1632, gen(nbcoronadodistprecise);
gen Wnbcoronadodist=round(nbcoronadodistprecise,1);
gen Qnbcoronadonear=outerlimit-nbcoronadodist;
recode Qnbcoronadonear -100000/0=0;

/*18 Naval Base Loma (CA) */
geodist lat lng 32.6875 -117.2396, gen(nblomadistprecise);
gen Wnblomadist=round(nblomadistprecise,1);
gen Qnblomanear=outerlimit-nblomadist;
recode Qnblomanear -100000/0=0;

/*19 Naval Base San Diego (CA) */
*gen nbsandiegolat = 32.683864 ;
*gen nbsandiegolng = -117.125710 ;
geodist lat lng 32.6839 -117.1257, gen(nbsandiegodistprecise);
gen Wnbsandiegodist=round(nbsandiegodistprecise,1);
gen Qnbsandiegonear=outerlimit-nbsandiegodist;
recode Qnbsandiegonear -100000/0=0;

/*20 Naval Base Ventura County (CA) */
*gen nbventuralat = 34.119979 ;
*gen nbventuralng = -119.102858 ;
geodist lat lng 34.1200 -119.1029, gen(nbventuradistprecise);
gen Wnbventuradist=round(nbventuradistprecise,1);
gen Qnbventuranear=outerlimit-nbventuradist;
recode Qnbventuranear -100000/0=0;

/*21 Travis AFB (CA) */
*gen travisafblat = 38.272072 ;
*gen travisafblng = -121.939959 ;
geodist lat lng 38.2721 -121.9400, gen(travisafbdistprecise);
gen Wtravisafbdist=round(travisafbdistprecise,1);
gen Qtravisafbnear=outerlimit-travisafbdist;
recode Qtravisafbnear -100000/0=0;

/*22 Vandenberg AFB */
*gen vandenbergafblat = 34.733662 ;
*gen vandenbergafblng = -120.537020 ;
geodist lat lng 34.7337 -120.5370, gen(vandenbergafbdistprecise);
gen Wvandenbergafbdist=round(vandenbergafbdistprecise,1);
gen Qvandenbergafbnear=outerlimit-vandenbergafbdist;
recode Qvandenbergafbnear -100000/0=0;

/* 23 USAFA */
*gen usafalat = 39.009329 ;
*gen usafalng = -104.886492 ;
geodist lat lng 39.0093 -104.8865, gen(usafadistprecise);
gen Wusafadist=round(usafadistprecise,1);
gen Qusafanear=outerlimit-usafadist;
recode Qusafanear -100000/0=0;

/*24 Buckley SFB (CO & maybe WY ) */
*gen buckleyafblat = 39.713800 ;
*gen buckleyafblng = -104.772488 ;
geodist lat lng 39.7138 -104.7725, gen(buckleysfbdistprecise);
gen Wbuckleysfbdist=round(buckleysfbdistprecise,1);
gen Qbuckleysfbnear=outerlimit-buckleysfbdist;
recode Qbuckleysfbnear -100000/0=0;

/*25 Fort Carson (CO only) */
*gen fortcarsonlat = 38.738681 ;
*gen fortcarsonlng = -104.786991 ;
geodist lat lng 38.7387 -104.7870, gen(fortcarsondistprecise);
gen Wfortcarsondist=round(fortcarsondistprecise,1);
gen Qfortcarsonnear=outerlimit-fortcarsondist;
recode Qfortcarsonnear -100000/0=0;

/*26 Peterson SFB (CO) */
*gen petersonsfblat = 38.826004 ;
*gen petersonsfblng = -104.701458 ;
geodist lat lng 38.8260 -104.7015, gen(petersonsfbdistprecise);
gen Wpetersonsfbdist=round(petersonsfbdistprecise,1);
gen Qpetersonsfbnear=outerlimit-petersonsfbdist;
recode Qpetersonsfbnear -100000/0=0;

/*27 Schriever AFB (CO) */
*gen schrieversfblat = 38.807389 ;
*gen schrieversfblng = -104.527051 ;
geodist lat lng 38.8074 -104.5271, gen(schrieversfbdistprecise);
gen Wschrieversfbdist=round(schrieversfbdistprecise,1);
gen Qschrieversfbnear=outerlimit-schrieversfbdist;
recode Qschrieversfbnear -100000/0=0;

/*28 Naval Sub Base New London Groton (CT...? ) */
*gen grotonlat = 41.392216 ;
*gen grotonlng = -72.087306 ;
geodist lat lng 41.3922 -72.0873, gen(grotondistprecise);
gen Wgrotondist=round(grotondistprecise,1);
gen Qgrotonnear=outerlimit-grotondist;
recode Qgrotonnear -100000/0=0;

/*29 Dover AFB (DEL MAr VA + PA NJ) */
*gen doverafblat = 39.124152 ;
*gen doverafblng = -75.474732 ;
geodist lat lng 39.1242 -75.4747, gen(doverafbdistprecise);
gen Wdoverafbdist=round(doverafbdistprecise,1);
gen Qdoverafbnear=outerlimit-doverafbdist;
recode Qdoverafbnear -100000/0=0;

/*30 JB Anacostia Bolling (DC MD VA ) */
*gen jbanacostiabollinglat = 38.841009 ;
*gen jbanacostiabollinglng = -77.014044 ;
geodist lat lng 38.8410 -77.0140, gen(jbanacostiabollingdistprecise);
gen Wjbanacostiabollingdist=round(jbanacostiabollingdistprecise,1);
gen Qjbanacostiabollingnear=outerlimit-jbanacostiabollingdist;
recode Qjbanacostiabollingnear -100000/0=0;

/*31 Eglin AFB (FL & AL) */
*gen eglinafblat = 30.479201 ;
*gen eglinafblng = -86.504396 ;
geodist lat lng 30.4792 -86.5044, gen(eglinafblatdistprecise);
gen Weglinafblatdist=round(eglinafblatdistprecise,1);
gen Qeglinafblatnear=outerlimit-eglinafblatdist;
recode Qeglinafblatnear -100000/0=0;

/*32 MacDill AFB (FL) */
*gen macdillafblat = 27.852540 ;
*gen macdillafblnt = -82.486009 ;
geodist lat lng 27.8525 -82.4860, gen(macdillafbdistprecise);
gen Wmacdillafbdist= round(macdillafbdistprecise,1);
gen Qmacdillafbnear=outerlimit-macdillafbdist;
recode Qmacdillafbnear -100000/0=0;

/*33 Patrick SFB (FL) */
*gen patricksfblat = 28.224075 ;
*gen patricksfblng = -80.603759 ;
geodist lat lng 28.2241 -80.6038, gen(patricksfbdistprecise);
gen Wpatricksfbdist=round(patricksfbdistprecise,1);
gen Qpatricksfbnear=outerlimit-patricksfbdist;
recode Qpatricksfbnear -100000/0=0;

/*34 Tyndall AFB (FL & AL & GA)  */

*gen tyndallafblat = 30.067899 ;
*gen tyndallafblng = -85.592143 ;
geodist lat lng 30.0679 -85.5921, gen(tyndallafbdistprecise);
gen Wtyndallafbdist=round(tyndallafbdistprecise,1);
gen Qtyndallafbnear=outerlimit-tyndallafbdist;
recode Qtyndallafbnear -100000/0=0;

/*35 Fort Moore née Fort Benning (GA & AL) */

*gen fortmoorelat = 32.356702 ;
*gen fortmoorelng = -84.965958 ;
geodist lat lng 32.3567 -84.9660, gen(fortmooredistprecise);
gen Wfortmooredist=round(fortmooredistprecise,1);
gen Qfortmoorenear =outerlimit - fortmooredist;
recode Qfortmoorenear -100000/0=0;

/*36 Fort Eisenhower née Fort Gordon (GA & SC ) */

*gen forteisenhowerlat = 33.422695 ;
*gen forteisenhowerlng = -82.144813 ;
geodist lat lng 33.4227 -82.1448, gen(forteisenhowerdistprecise);
gen Wforteisenhowerdist = round(forteisenhowerdistprecise,1);
gen Qforteisenhowernear = outerlimit - forteisenhowerdist;
recode Qforteisenhowernear -100000/0=0;

/*37 Fort Stewart (GA & SC) */

*gen fortstewartlat = 31.868003 ;
*gen fortstewartlng = -81.609853 ;
geodist lat lng 31.8680 -81.6099, gen(fortstewartdistprecise);
gen Wfortstewartdist=round(fortstewartdistprecise,1);
gen Qfortstewartnear=outerlimit-fortstewartdist;
recode Qfortstewartnear -100000/0=0;

/*38 Moody AFB (GA & FL) */

*gen moodyafblat = 30.976176 ;
*gen moodyafblng = -83.205361 ;
geodist lat lng 30.9762 -83.2054, gen(moodyafbdistprecise);
gen Wmoodyafbdist = round(moodyafbdistprecise,1);
gen Qmoodyafbnear = outerlimit-moodyafbdist;
recode Qmoodyafbnear -100000/0=0;

/*39 Naval Sub Base King's Bay (GA & FL) */

*gen nsbkingsbaylat = 30.792295 ;
*gen nsbkingsbaylng = -81.565519 ;
geodist lat lng 30.7923 -81.5655, gen(nsbkingsbaydistprecise);
gen Wnsbkingsbaydist = round(nsbkingsbaydistprecise,1);
gen Qnsbkingsbaynear = outerlimit-nsbkingsbaydist;
recode Qnsbkingsbaynear -100000/0=0;

/*40 Robins AFB GA */

*gen robinsafblat = 32.624482 ;
*gen robinsafblng = -83.594247 ;
geodist lat lng 32.6245 -83.5943, gen(robinsafbdistprecise);
gen Wrobinsafbdist = round(robinsafbdistprecise);
gen Qrobinsafbnear = outerlimit - robinsafbdist ;
recode Qrobinsafbnear -100000/0=0;

/*41 Pearl Harbor Hickam HI */

*gen pearlharborlat = 21.354916 ;
*gen pearlharborlng = -157.942320 ;
geodist lat lng 21.3549 -157.9423, gen(pearlharbordistprecise);
gen Wpearlharbordist = round(pearlharbordistprecise,1);
gen Qpearlharbornear = outerlimit-pearlharbordist;
recode Qpearlharbornear -100000/0=0;

/*42 Mountain Home AFB (ID   ) */
*gen mountainhomeafblat = 43.055529 ;
*gen mountainhomeafblng = -115.859672 ;
geodist lat lng 43.0555 -115.8597, gen(mountainhomeafbdistprecise);
gen Wmountainhomeafbdist = round(mountainhomeafbdistprecise,1);
gen Qmountainhomeafbnear = outerlimit-mountainhomeafbdist;
recode Qmountainhomeafbnear -100000/0=0;

/*43 Scott AFB (IL & MO) */
*gen scottafblat = 38.538999 ;
*gen scottafblng = -89.864664 ;
geodist lat lng 38.5390 -89.8647, gen(scottafbdistprecise);
gen Wscottafbdist = round(scottafbdistprecise, 1);
gen Qscottafbnear = outerlimit-scottafbdist ;
recode Qscottafbnear -100000/0=0;

/*44 Fort Leavenworth (KS & MO)  */
*gen fortleavenworthlat = 39.352477 ;
*gen fortleavenworthlng = -94.917856 ;
geodist lat lng 39.3525 -94.9179, gen(fortleavenworthdistprecise);
gen Wfortleavenworthdist = round(fortleavenworthdistprecise, 1);
gen Qfortleavenworthnear = outerlimit-fortleavenworthdist;
recode Qfortleavenworthnear -100000/0=0;

/*45 Fort Riley (KS) (NE ~60 miles away) */
*gen fortrileylat = 39.113726 ;
*gen fortrileylng = -96.813747 ;
geodist lat lng 39.1137 -96.8138, gen(fortrileydistprecise);
gen Wfortrileydist= round(fortrileydistprecise, 1);
gen Qfortrileynear = outerlimit-fortrileydist;
recode Qfortrileynear -100000/0=0;

/* 46 McConnell AFB (KS & OK) */
*gen mcconnellafblat = 37.632485 ;
*gen mcconnellafblng = -97.254940 ;
geodist lat lng 37.6325 -97.2549, gen(mcconnellafbdistprecise);
gen Wmcconnellafbdist = round(mcconnellafbdistprecise, 1);
gen Qmcconnellafbnear = outerlimit - mcconnellafbdist ;
recode Qmcconnellafbnear -100000/0=0;

/*47 Fort Campbell (KY & TN)   */
*gen fortcampbelllat = 36.656181 ;
*gen fortcampbelllng = -87.472468 ;
geodist lat lng 36.6562 -87.4725, gen(fortcampbelldistprecise);
gen Wfortcampbelldist = round(fortcampbelldistprecise, 1);
gen Qfortcampbellnear = outerlimit - fortcampbelldist ;
recode Qfortcampbellnear -100000/0 = 0;

/*48 Fort Knox (KY   */
*gen fortknoxlat = 37.912776 ;
*gen fortknoxlng = -85.953635 ;
geodist lat lng 37.9128 -85.9536, gen(fortknoxdistprecise);
gen Wfortknoxdist = round(fortknoxdistprecise, 1);
gen Qfortknoxnear = outerlimit-fortknoxdist ;
recode Qfortknoxnear -100000/0 = 0;

/*49 Barksdale AFB (LA & TX & AR) */
*gen barksdaleafblat = 32.502917 ;
*gen barksdaleafblng = -93.680447 ;
geodist lat lng 32.5029 -93.6805, gen(barksdaleafbdistprecise);
gen Wbarksdaleafbdist = round(barksdaleafbdistprecise, 1);
gen Qbarksdaleafbnear = outerlimit-barksdaleafbdist;
recode Qbarksdaleafbnear -100000/0=0;

/*50 Fort Johnson née Fort Polk (LA & TX) */
*gen fortjohnsonlat = 31.048114 ;
*gen fortjohnsonlng = -93.207835 ;
geodist lat lng 31.048114 -93.207835, gen(fortjohnsondistprecise);
gen Wfortjohnsondist = round(fortjohnsondistprecise, 1);
gen Qfortjohnsonnear = outerlimit-fortjohnsondist;
recode Qfortjohnsonnear -100000/0=0;

/*51 USNA (MD, DC, VA, DE) */
*gen usnalat = 38.982852 ;
*gen usnalng = -76.485075 ;
geodist lat lng 38.9829 -76.4851, gen(usnadistprecise);
gen Wusnadist = round(usnadistprecise, 1);
gen Qusnanear = outerlimit-usnadist;
recode Qusnanear -100000/0=0 ;

/*52 Fort Detrick (MD, PA, VA, DC) */
*gen fortdetricklat = 39.437572 ;
*gen fortdetricklng = -77.425105 ;
geodist lat lng 39.4376 -77.4251, gen(fortdetrickdistprecise);
gen Wfortdetrickdist = round(fortdetrickdistprecise, 1);
gen Qfortdetricknear = outerlimit-fortdetrickdist;
recode Qfortdetricknear -100000/0=0;

/*53 Fort George Meade (MD DC VA)  */
*gen fortgeorgemeadelat = 39.103356 ;
*gen fortgeorgemeadelng = -76.740421 ;
geodist lat lng 39.1034 -76.7404, gen(fortgeorgemeadedistprecise);
gen Wfortgeorgemeadedist = round(fortgeorgemeadedistprecise, 1);
gen Qfortgeorgemeadenear = outerlimit-fortgeorgemeadedist;
recode Qfortgeorgemeadenear -100000/0=0;

/*54 Andrews AFB (MD DC VA) (err, JB Andrews) */
*gen andrewsafblat = 38.812662 ;
*gen andrewsafblng = -76.884506 ;
geodist lat lng 38.8127 -76.8845, gen(andrewsafbdistprecise);
gen Wandrewsafbdist = round(andrewsafbdistprecise, 1);
gen Qandrewsafbnear = outerlimit-andrewsafbdist;
recode Qandrewsafbnear -100000/0=0;

/*55 Hanscom AFB (MA & NH)   */
*gen hanscomafblat = 42.459967 ;
*gen hanscomafblng = -71.279684 ;
geodist lat lng 42.4591 -71.2825, gen(hanscomafbdistprecise);
gen Whanscomafbdist = round(hanscomafbdistprecise, 1);
gen Qhanscomafbnear = outerlimit-hanscomafbdist;
recode Qhanscomafbnear -100000/0=0;

/*56 Fort Devens (MA & NH) */
*gen fortdevenslat = 42.537273 ;
*gen fortdevenslng = -71.618958 ;
geodist lat lng 42.5373 -71.6190, gen(fortdevensdistprecise);
gen Wfortdevensdist = round(fortdevensdistprecise, 1);
gen Qfortdevensnear = outerlimit-fortdevensdist;
recode Qfortdevensnear -100000/0=0;

/*57 Columbus AFB (MS & AL)  */
*gen columbusafblat = 33.630177 ;
*gen columbusafblng = -88.447216 ;
geodist lat lng 33.6302 -88.4472, gen(columbusafbdistprecise);
gen Wcolumbusafbdist = round(columbusafbdistprecise, 1);
gen Qcolumbusafbnear = outerlimit-columbusafbdist;
recode Qcolumbusafbnear -100000/0=0;

/*58 Keesler AFB (MS */
*gen keeslerafblat = 30.405798 ;
*gen keeslerafblng = -88.913334 ;
geodist lat lng 30.4058 -88.9133, gen(keeslerafbdistprecise);
gen Wkeeslerafbdist = round(keeslerafbdistprecise, 1);
gen Qkeeslerafbnear = outerlimit - keeslerafbdist ;
recode Qkeeslerafbnear -100000/0=0;

/*59 Fort Leonard Wood  (MO) */
*gen fortleonardwoodlat = 37.760341 ;
*gen fortleonardwoodlng = -92.117317 ;
geodist lat lng 37.7603 -92.1173, gen(fortleonardwooddistprecise);
gen Wfortleonardwooddist = round(fortleonardwooddistprecise, 1);
gen Qfortleonardwoodnear = outerlimit - fortleonardwooddist ;
recode Qfortleonardwoodnear -100000/0=0;

/*60 Whiteman AFB  (MO and maybe KS)  */
*gen whitemanafblat = 38.729131 ;
*gen whitemanafblng = -93.565792 ;
geodist lat lng 38.7291 -93.5658, gen(whitemanafbdistprecise);
gen Wwhitemanafbdist = round(whitemanafbdistprecise, 1);
gen Qwhitemanafbnear = outerlimit - whitemanafbdist ;
recode Qwhitemanafbnear -100000/0=0;

/*61 Malmstrom AFB (MT)   */
*gen malmstromafblat = 47.513533 ;
*gen malmstromafblng = -111.195658 ;
geodist lat lng 47.5135 -111.1957, gen(malmstromafbdistprecise);
gen Wmalmstromafbdist = round(malmstromafbdistprecise, 1);
gen Qmalmstromafbnear = outerlimit - malmstromafbdist;
recode Qmalmstromafbnear -100000/0=0;

/*62 Offutt AFB (NE & IA & MO  */
*gen offuttafblat = 41.114777 ;
*gen offuttafblng = -95.919478 ;
geodist lat lng 41.1148 -95.9195, gen(offuttafbdistprecise) ;
gen Woffuttafbdist = round(offuttafbdistprecise, 1);
gen Qoffuttafbnear = outerlimit-offuttafbdist ;
recode Qoffuttafbnear -100000/0=0 ;

/*63 Creech AFB (NV & CA)  */
*gen creechafblat = 36.591804 ;
*gen creechafblng = -115.662915 ;
geodist lat lng 36.5918 -115.6629 , gen(creechafbdistprecise);
gen Wcreechafbdist = round(creechafbdistprecise, 1);
gen Qcreechafbnear = outerlimit - creechafbdist ;
recode Qcreechafbnear -100000/0=0 ;

/*64 Nellis AFB (NV & CA)   */
*gen nellisafblat = 36.240257 ;
*gen nellisafblng = -115.048620 ;
geodist lat lng 36.2403 -115.0486, gen(nellisafbdistprecise);
gen Wnellisafbdist = round(nellisafbdistprecise, 1);
gen Qnellisafbnear = outerlimit-nellisafbdist;
recode Qnellisafbnear -100000/0=0;

/*65 JB McGuire Dix (NJ & PA)  */
*gen fortdixlat = 40.024350 ;
*gen fortdixlng = -74.602578 ;
geodist lat lng 40.0244 -74.6026, gen(fortdixdistprecise);
gen Wfortdixdist = round(fortdixdistprecise, 1) ;
gen Qfortdixnear = outerlimit - fortdixdist ;
recode Qfortdixnear -100000/0=0;

/*66 JB Lakehurst NJ & PA (technically part of JB mcguire dix lakehurst, but it's down the road */
*gen jblakehurstlat = 40.030772 ;
*gen jblakehurstlng = -74.316683 ;
geodist lat lng 40.0308 -74.3167, gen(jblakehurstdistprecise) ;
gen Wjblakehurstdist = round(jblakehurstdistprecise, 1);
gen Qjblakehurstnear = outerlimit - jblakehurstdist ;
recode Qjblakehurstnear -100000/0=0 ;

/*67 Cannon AFB (NM & TX)  */
*gen cannonafblat = 34.391336 ;
*gen cannonafblng = -103.327262 ;
geodist lat lng 34.3913 -103.3273, gen(cannonafbdistprecise);
gen Wcannonafbdist = round(cannonafbdistprecise, 1);
gen Qcannonafbnear = outerlimit - cannonafbdist ;
recode Qcannonafbnear -100000/0=0 ;

/*68 Holloman AFB (NM only probably; El Paso is >  50 miles away  */
*gen hollomanafblat = 32.838308 ;
*gen hollomanafblng = -106.082016 ;
geodist lat lng 32.8383 -106.0820, gen(hollomanafbdistprecise);
gen Whollomanafbdist = round(hollomanafbdistprecise, 1);
gen Qhollomanafbnear = outerlimit-hollomanafbdist ;
recode Qhollomanafbnear -100000/0=0;

/*69 Kirtland AFB (NM only) */
*gen kirtlandafblat = 35.057498 ;
*gen kirtlandafblng = -106.556676 ;
geodist lat lng 35.0575 -106.5567, gen(kirtlandafbdistprecise);
gen Wkirtlandafbdist = round(kirtlandafbdistprecise, 1);
gen Qkirtlandafbnear = outerlimit - kirtlandafbdist ;
recode Qkirtlandafbnear -100000/0=0 ;

/*70 USMA  (NY NJ) */
*gen usmalat = 41.391649 ;
*gen usmalng = -73.956260 ;
geodist lat lng 41.3917 -73.9563, gen(usmadistprecise);
gen Wusmadist = round(usmadistprecise, 1);
gen Qusmanear = outerlimit - usmadist ;
recode Qusmanear -100000/0=0;

/*71 Fort Drum (NY)   */
*gen fortdrumlat = 44.049779 ;
*gen fortdrumlng = -75.787888 ;
geodist lat lng 44.0498 -75.7879, gen(fortdrumdistprecise);
gen Wfortdrumdist = round(fortdrumdistprecise, 1);
gen Qfortdrumnear = outerlimit - fortdrumdist ;
recode Qfortdrumnear -100000/0=0;

/*72 Fort Hamilton (NY)  */
*gen forthamiltonlat = 40.608425 ;
*gen forthamiltonlng = -74.028446 ;
geodist lat lng 40.6084 -74.0284, gen(forthamiltondistprecise);
gen Wforthamiltondist = round(forthamiltondistprecise, 1);
gen Qforthamiltonnear = outerlimit - forthamiltondist ;
recode Qforthamiltonnear -100000/0=0 ;

/*73 Camp LeJeune (NC)  */
*gen camplejeunelat = 34.583710 ;
*gen camplejeunelng = -77.359013 ;
geodist lat lng 34.5837 -77.3590, gen(camplejeunedistprecise);
gen Wcamplejeunedist = round(camplejeunedistprecise, 1);
gen Qcamplejeunenear = outerlimit - camplejeunedist ;
recode Qcamplejeunenear -100000/0=0;

/*74 Fort Liberty née Fort Bragg (NC & SC [just] )   */
*gen fortlibertylat = 35.139407 ;
*gen fortlibertylng = -79.005207 ;
geodist lat lng 35.1394 -79.0052, gen(fortlibertydistprecise) ;
gen Wfortlibertydist = round(fortlibertydistprecise, 1);
gen Qfortlibertynear = outerlimit - fortlibertydist ;
recode Qfortlibertynear -100000/0=0;

/*75 Seymour Johnson AFB (NC)   */
*gen seymourjohnsonafblat = 35.347204 ;
*gen seymourjohnsonafblng = -77.965230 ;
geodist lat lng 35.3472 -77.9652, gen(seymourjohnsonafbdistprecise);
gen Wseymourjohnsonafbdist = round(seymourjohnsonafbdistprecise, 1);
gen Qseymourjohnsonafbnear = outerlimit - seymourjohnsonafbdist ;
recode Qseymourjohnsonafbnear -100000/0=0;

/*76 Grand Forks AFB (ND & MN )   */
*gen grandforksafblat= 47.950233 ;
*gen grandforksafblng = -97.377769 ;
geodist lat lng 47.9502 -97.3778, gen(grandforksafbdistprecise);
gen Wgrandforksafbdist = round(grandforksafbdistprecise);
gen Qgrandforksafbnear = outerlimit - grandforksafbdist ;
recode Qgrandforksafbnear -100000/0=0 ;

/*77 Minot AFB (ND)  */
*gen minotafblat = 48.422270 ;
*gen minotafblng = -101.333567 ;
geodist lat lng 48.4223 -101.3336, gen(minotafbdistprecise);
gen Wminotafbdist = round(minotafbdistprecise, 1);
gen Qminotafbnear = outerlimit - minotafbdist ;
recode Qminotafbnear -100000/0=0;

/*78 Wright Patterson AFB  (OH & IN) */
*gen wrightpattersonafblat = 39.809792 ;
*gen wrightpattersonafblng = -84.035899 ;
geodist lat lng 39.8098 -84.0359, gen(wrightpattersonafbdistprecise);
gen Wwrightpattersonafbdist = round(wrightpattersonafbdistprecise, 1);
gen Qwrightpattersonafbnear = outerlimit - wrightpattersonafbdist ;
recode Qwrightpattersonafbnear -100000/0=0 ;

/*79 Altus AFB (OK & TX) */
*gen altusafblat = 34.655892 ;
*gen altusafblng = -99.285599 ;
geodist lat lng 34.6559 -99.2856 , gen(altusafbdistprecise);
gen Waltusafbdist = round(altusafbdistprecise, 1);
gen Qaltusafbnear = outerlimit - altusafbdist ;
recode Qaltusafbnear -100000/0 = 0;

/*80 Fort Sill (OK & TX)  */
*gen fortsilllat = 34.661542 ;
*gen fortsilllng = -98.415008 ;
geodist lat lng 34.6615 -98.4150, gen(fortsilldistprecise);
gen Wfortsilldist = round(fortsilldistprecise, 1);
gen Qfortsillnear = outerlimit - fortsilldist ;
recode Qfortsillnear -100000/0 = 0 ;

/*81 Tinker AFB (OK)  */
*gen tinkerafblat = 35.428128 ;
*gen tinkerafblng = -97.398987 ;
geodist lat lng 35.4281 -97.3990, gen(tinkerafbdistprecise) ;
gen Wtinkerafbdist = round(tinkerafbdistprecise, 1);
gen Qtinkerafbnear = outerlimit - tinkerafbdist ;
recode Qtinkerafbnear -100000/0=0;

/*82 Vance AFB (OK & KS) */
gen vanceafblat = 36.340744 ;
gen vanceafblng = -97.903106 ;
geodist lat lng 36.3407 -97.9031, gen(vanceafbdistprecise);
gen Wvanceafbdist = round(vanceafbdistprecise, 1);
gen Qvanceafbnear = outerlimit - vanceafbdist ;
recode Qvanceafbnear -100000/0=0 ;

/*83 Fort Jackson (SC & GA) */
gen fortjacksonlat = 34.011553 ;
gen fortjacksonlng = -80.944232 ;
geodist lat lng 34.0116 -80.9442, gen(fortjacksondistprecise);
gen Wfortjacksondist = round(fortjacksondistprecise, 1);
gen Qfortjacksonnear = outerlimit - fortjacksondist ;
recode Qfortjacksonnear -100000/0=0 ;

/*84 Joint Base Charleston (SC) */
gen jbcharlestonlat = 32.896266 ;
gen jbcharlestonlng = -80.056203 ;
geodist lat lng 32.8963 -80.0562, gen(jbcharlestondistprecise);
gen Wjbcharlestondist = round(jbcharlestondistprecise, 1);
gen Qjbcharlestonnear = outerlimit - jbcharlestondist ;
recode Qjbcharlestonnear -100000/0 = 0 ;

/*85 Shaw Air Force Base  (SC)   */
gen shawafblat = 33.969273 ;
gen shawafblng = -80.483949 ;
geodist lat lng 33.9693 -80.4840, gen(shawafbdistprecise);
gen Wshawafbdist = round(shawafbdistprecise, 1);
gen Qshawafbnear = outerlimit - shawafbdist ;
recode Qshawafbnear -100000/0 = 0 ;

/*86 Ellsworth Air Force Base (SD) */
gen ellsworthafblat = 44.132964 ;
gen ellsworthafblng = -103.072673 ;
geodist lat lng 44.1330 -103.0727, gen(ellsworthafbdistprecise);
gen Wellsworthafbdist = round(ellsworthafbdistprecise, 1);
gen Qellsworthafbnear = outerlimit - ellsworthafbdist ;
recode Qellsworthafbnear -100000/0 = 0 ;

/*87 Arnold AFB (TN & AL & GA) */
gen arnoldafblat = 35.383054 ;
gen arnoldafblng = -86.030087 ;
geodist lat lng 35.3831 -86.0301, gen(arnoldafbdistprecise) ;
gen Warnoldafbdist = round(arnoldafbdistprecise, 1);
gen Qarnoldafbnear = outerlimit - arnoldafbdist ;
recode Qarnoldafbnear -100000/0=0 ;

/*88 Dyess AFB (TX)  */
gen dyessafblat = 32.423795 ;
gen dyessafblng = -99.845607 ;
geodist lat lng 32.4238 -99.8457, gen(dyessafbdistprecise);
gen Wdyessafbdist = round(dyessafbdistprecise, 1);
gen Qdyessafbnear = outerlimit - dyessafbdist ;
recode Qdyessafbnear -100000/0=0 ;

/*89 Fort Bliss (TX & NM) */
*gen fortblisslat = 31.813196 ;
*gen fortblisslng = -106.414385 ;
geodist lat lng 31.8132 -106.4144, gen(fortblissdistprecise);
gen Wfortblissdist = round(fortblissdistprecise, 1);
gen Qfortblissnear = outerlimit - fortblissdist ;
recode Qfortblissnear -100000/0 = 0 ;

/*90 Fort Cavazos née Fort Hood (TX) */
*gen fortcavazoslat = 31.134763 ;
*gen fortcavazoslng = -97.770486 ;
geodist lat lng 31.1348 -97.7705, gen(fortcavazosdistprecise);
gen Wfortcavazosdist = round(fortcavazosdistprecise, 1);
gen Qfortcavazosnear = outerlimit - fortcavazosdist ;
recode Qfortcavazosnear -100000/0 = 0;

/*91 Goodfellow AFB (TX)  */
gen goodfellowafblat = 31.432426 ;
gen goodfellowafblng = -100.404605 ;
geodist lat lng 31.4324 -100.4046, gen(goodfellowafbdistprecise);
gen Wgoodfellowafbdist = round(goodfellowafbdistprecise, 1);
gen Qgoodfellowafbnear = outerlimit - goodfellowafbdist ;
recode Qgoodfellowafbnear -100000/0=0 ;

/*92 Fort Sam Houston (TX) (JB San Anton) */
gen fortsamlat = 29.451106 ;
gen fortsamlng = -98.452406 ;
geodist lat lng 29.4512 -98.4524, gen(fortsamdistprecise);
gen Wfortsamdist = round(fortsamdistprecise, 1);
gen Qfortsamnear = outerlimit - fortsamdist ;
recode Qfortsamnear -100000/0=0 ;

/*93 Lackland AFB (TX) (JB San Anton) */
gen lacklandafblat = 29.388958 ;
gen lacklandafblng = -98.626196 ;
geodist lat lng 29.3890 -98.6262, gen(lacklandafbdistprecise);
gen Wlacklandafbdist = round(lacklandafbdistprecise, 1);
gen Qlacklandafbnear = outerlimit - lacklandafbdist ;
recode Qlacklandafbnear -100000/0 = 0;

/*94 Randolph AFB (TX) (JB San Anton) */
gen randolphafblat = 29.532181 ;
gen randolphafblng = -98.280194 ;
geodist lat lng 29.5322 -98.2802, gen(randolphafbdistprecise);
gen Wrandolphafbdist = round(randolphafbdistprecise, 1);
gen Qrandolphafbnear = outerlimit - randolphafbdist ;
recode Qrandolphafbnear -100000/0 = 0 ;

/*95 Camp Bullis (TX) might just be an emptyish range*/
gen campbullislat = 29.638919 ;
gen campbullislng = -98.580428 ;
geodist lat lng 29.6389 -98.5804, gen(campbullisdistprecise);
gen Wcampbullisdist = round(campbullisdistprecise, 1);
gen Qcampbullisnear = outerlimit - campbullisdist ;
recode Qcampbullisnear -100000/0 = 0;

/*96 Martindale Army Air Field (TX) */
gen martindaleaaflat = 29.435956 ;
gen martindaleaaflng = -98.380737 ;
geodist lat lng 29.4360 -98.3807, gen(martindaleaafdistprecise) ;
gen Wmartindaleaafdist = round(martindaleaafdistprecise, 1);
gen Qmartindaleaafnear = outerlimit - martindaleaafdist ;
recode Qmartindaleaafnear -100000/0 = 0 ;

/*97 Laughlin AFB (TX) */
gen laughlinafblat = 29.356806 ;
gen laughlinafblng = -100.787785 ;
geodist lat lng 29.3568 -100.7878, gen(laughlinafbdistprecise);
gen Wlaughlinafbdist = round(laughlinafbdistprecise, 1);
gen Qlaughlinafbnear = outerlimit - laughlinafbdist ;
recode Qlaughlinafbnear -100000/0 = 0;

/*98 Sheppard AFB (TX & OK) */
gen sheppardafblat = 33.983378 ;
gen sheppardafblng = -98.499761 ;
geodist lat lng 33.9834 -98.4998, gen(sheppardafbdistprecise);
gen Wsheppardafbdist = round(sheppardafbdistprecise, 1);
gen Qsheppardafbnear = outerlimit - sheppardafbdist ;
recode Qsheppardafbnear -100000/0=0;

/*99 Hill AFB (UT & WY & ID) */
*gen hillafblat = 41.111056 ;
*gen hillafblng = -111.980520 ;
geodist lat lng 41.1111 -111.9805, gen(hillafbdistprecise);
gen Whillafbdist = round(hillafbdistprecise, 1);
gen Qhillafbnear = outerlimit - hillafbdist ;
recode Qhillafbnear -1000000/0 = 0 ;

/*100 Fort Belvoir (VA & MD & DC) */
*gen fortbelvoirlat = 38.706635 ;
*gen fortbelvoirlng = -77.144956 ;
geodist lat lng 38.7080 -77.1416, gen(fortbelvoirdistprecise) ;
gen Wfortbelvoirdist = round(fortbelvoirdistprecise, 1);
gen Qfortbelvoirnear = outerlimit - fortbelvoirdist ;
recode Qfortbelvoirnear -100000/0 = 0;

/*101 Fort Gregg-Adams née Fort Lee (VA)  */
*gen fortgreggadamslat = 37.237689 ;
*gen fortgreggadamslng = -77.338228 ;
geodist lat lng 37.2377 -77.3382, gen(fortgreggadamsdistprecise);
gen Wfortgreggadamsdist = round(fortgreggadamsdistprecise, 1);
gen Qfortgreggadamsnear = outerlimit - fortgreggadamsdist ;
recode Qfortgreggadamsnear -100000/0 = 0;

/*102 JB Myer-Henderson Hall (DC MD VA) */
*gen fortmyerlat = 38.872740 ;
*gen fortmyerlng = -77.079062 ;
geodist lat lng 38.8727 -77.0791 , gen(fortmyerdistprecise);
gen Wfortmyerdist = round(fortmyerdistprecise, 1);
gen Qfortmyernear = outerlimit - fortmyerdist ;
recode Qfortmyernear -100000/0 = 0 ;

/* 103 Fort McNair (tech. in JB Myer Henderson Hall ) VA DC MD */
*gen fortmcnairlat = 38.867808 ;
*gen fortmcnairlng = -77.0104846 ;
geodist lat lng 38.8678 -77.01048, gen(fortmcnairdistprecise);
gen Wfortmcnairdist = round(fortmcnairdistprecise, 1);
gen Qfortmcnairnear = outerlimit - fortmcnairdist ;
recode Qfortmcnairnear -100000/0 = 0 ;

/* 104 Langley AFB (VA) */
*gen langleyafblat = 37.075179 ;
*gen langleyafblng = -76.353573 ;
geodist lat lng 37.0752 -76.3536, gen(langleyafbdistprecise);
gen Wlangleyafbdist = round(langleyafbdistprecise, 1);
gen Qlangleyafbnear = outerlimit - langleyafbdist ;
recode Qlangleyafbnear -100000/0 = 0;

/*105 Fort Eustis (VA) tech. part of JB Langley-Eustis */
*gen forteustislat = 37.160147 ;
*gen forteustislng = -76.576153 ;
geodist lat lng 37.1601 -76.5762, gen(forteustisdistprecise);
gen Wforteustisdist = round(forteustisdistprecise, 1);
gen Qforteustisnear = outerlimit - forteustisdist ;
recode Qforteustisnear -100000/0 = 0;

/*106 Fort Story (VA) tech part of  */
*gen fortstorylat = 36.929346 ;
*gen fortstorylng = -76.015772 ;
geodist lat lng 36.9293 -76.0158, gen(fortstorydistprecise);
gen Wfortstorydist = round(fortstorydistprecise, 1);
gen Qfortstorynear = outerlimit - fortstorydist ;
recode Qfortstorynear -100000/0 = 0 ;

/*107 Naval Expeditionary Base Little Creek (VA, tech part of Fort Story*/
*gen neblittlecreeklat = 36.914622 ;
*gen neblittlecreeklng = -76.185678 ;
geodist lat lng 36.9146 -76.1857, gen(neblittlecreekdistprecise);
gen Wneblittlecreekdist = round(neblittlecreekdistprecise, 1);
gen Qneblittlecreeknear = outerlimit - neblittlecreekdist ;
recode Qneblittlecreeknear -100000/0 = 0 ;

/*108 Marine Corps Base Quantico (VA  MD) */
*gen mcbquanticolat = 38.518527 ;
*gen mcbquanticolng = -77.292018 ;
geodist lat lng 38.5185 -77.2920, gen(mcbquanticodistprecise);
gen Wmcbquanticodist = round(mcbquanticodistprecise, 1);
gen Qmcbquanticonear = outerlimit - mcbquanticodist ;
recode Qmcbquanticonear -100000/0 = 0 ;

/*109 Fairchild AFB WA&ID */
*gen fairchildafblat = 47.636761 ;
*gen fairchildafblng = -117.643831 ;
geodist lat lng 47.6368 -117.6438, gen(fairchildafbdistprecise);
gen Wfairchildafbdist = round(fairchildafbdistprecise, 1);
gen Qfairchildafbnear = outerlimit - fairchildafbdist ;
recode Qfairchildafbnear -100000/0 = 0;

/*110 JB Lewis-McChord WA (close enough to be one point ) */
*gen jblewismcchordlat = 47.116255 ;
*gen jblewismcchordlng = -122.541056 ;
geodist lat lng 47.1163 -122.5411, gen(jblewismcchorddistprecise);
gen Wjblewismcchorddist = round(jblewismcchorddistprecise, 1);
gen Qjblewismcchordnear = outerlimit - jblewismcchorddist ;
recode Qjblewismcchordnear -100000/0 = 0 ;

/*111 Naval Base Kitsap WA */
*gen nbkitsaplat = 47.689564 ;
*gen nbkitsaplng = -122.708032 ;
geodist lat lng 47.6896 -122.7080, gen(nbkitsapdistprecise) ;
gen Wnbkitsapdist = round(nbkitsapdistprecise, 1);
gen Qnbkitsapnear = outerlimit - nbkitsapdist ;
recode Qnbkitsapnear -100000/0 = 0 ;

/*112 Fort McCoy (WI & MN) */
*gen fortmccoylat = 44.023859 ;
*gen fortmccoylng = -90.686654 ;
geodist lat lng 44.0239 -90.6867, gen(fortmccoydistprecise);
gen Wfortmccoydist = round(fortmccoydistprecise, 1);
gen Qfortmccoynear = outerlimit - fortmccoydist ;
recode Qfortmccoynear -100000/0 = 0 ;

/*113 F E Warren AFB (WY & CO & NE) */
*gen fewarrenafblat = 41.151971 ;
*gen fewarrenafblng = -104.856425 ;
geodist lat lng 41.1520 -104.8564, gen(fewarrenafbdistprecise);
gen Wfewarrenafbdist = round(fewarrenafbdistprecise, 1);
gen Qfewarrenafbnear = outerlimit - fewarrenafbdist ;
recode Qfewarrenafbnear -100000/0 = 0;

drop *precise ;
egen lowestdistance = rowmin(W*);
replace lowestdistance=1 if lowestdistance==0 ;
egen closest = rowmax(Q*);
egen lotsabase = rowtotal(Q*);

gen roundedclosest = round(closest, 10);
gen roundedlots = round(lotsabase, 10) ;

gen logdistclosest = ln(lowest);
gen roundedlogclosest = round(logdistclosest, 1);

#delimit ;
table inputstate_byte, statistic(mean year) statistic(count year) ;
tab inputstate_byte year, col m ;
table year, statistic(mean zipcode) statistic(count zipcode);
table year, statistic(mean roundedclosest) statistic(count roundedclosest);
table year, statistic(mean roundedlots) statistic(count roundedlots);
table year, stat(mean lowest) stat(count lowest) stat(min lowest) stat(max lowest);
#delimit ;
tab year gop2ptyvote , row m ;
tab year goppid , row m ;
tab year goppid_l , row m ;
tab year whitenonhisp , row m ;
tab year ba , row m ;
tab year male , row m ;
tab year bage , row m ;
tab year birthdecade , row m ;
tab year svyk , row m ;
tab year vet , row m ;
tab year evangc , row m ;
tab year marrspouse , row m ;

compress ;
save "`folderfile'ces06-22data_recoded.dta", replace ;
